package com.javaweb.config;

import com.javaweb.common.StringTools;
import com.javaweb.entity.*;
import com.javaweb.entity.User;
import com.javaweb.entity.Role;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Date;


@Slf4j
@Configuration
@ComponentScan(basePackages = "com.javaweb")
@PropertySource(value = "classpath:db.properties")
@Data
public class SpringConfig {
    @Value("${db.driver}")
    private String dbDriver;
    @Value("${db.user}")
    private String dbUser;
    @Value("${db.url}")
    private String dbUrl;
    @Value("${db.password}")
    private String dbPassword;

    @Bean("user1")
    @Scope("singleton")
    public User getUser1(){
        return new User("001","mao","123456","6666",
                "mao","mao@fnju.com","15005013791",1,20);
    }

    @Bean("user2")
    @Scope("prototype")
    public User getUser2(){
        return new User("001","mao","123456","6666",
                "mao","mao@fnju.com","15005013791",1,20);
    }
    //role
    @Bean("role1")
    @Scope("singleton")
    public Role getRole1(){
        return new Role(00001,"r00001","管理员",
                "管理员");
    }

    @Bean("role2")
    @Scope("prototype")
    public Role getRole2() {
        return new Role(00001, "r00001", "管理员",
                "管理员");
    }
    //userRole
    @Bean("userRole1")
    @Scope("singleton")
    public UserRole getUserRole1(@Autowired User user1,@Autowired Role role1){
        return new UserRole(00001,user1,role1);
    }

    @Bean("userRole2")
    @Scope("prototype")
    public UserRole getUserRole2(@Autowired User user2,@Autowired Role role2) {
        return new UserRole(00001,user2,role2);
    }
    //role_permission
    @Bean("rolePermission1")
    @Scope("singleton")
    public RolePermission getRolePermission1(@Autowired Role role1,@Autowired Permission permission1){
        return new RolePermission(00001,role1,permission1);
    }

    @Bean("rolePermission2")
    @Scope("prototype")
    public RolePermission getRolePermission2(@Autowired Role role2,@Autowired Permission permission2) {
        return new RolePermission(00001,role2,permission2);

    }
    //permission
    @Bean("permission1")
    @Scope("singleton")
    public Permission getPermission1(){
        return new Permission(00001,"p00001","添加","添加",
                "/add");
    }

    @Bean("permission2")
    @Scope("prototype")
    public Permission getPermission2() {
        return new Permission(00001,"p00001","添加","添加",
                "/add");
    }

    //数据库连接connection
    @Bean("connection")
    @Scope("prototype")
    public Connection getConnection() {
        try {
            log.info("数据库连接成功！");
            Class.forName(this.dbDriver) ;
            return DriverManager.getConnection(this.dbUrl,
                    this.dbUser,
                    this.dbPassword) ;
        } catch (Exception e) {
            log.error("Connection created failed !") ;
            return null ;
        }
    }

}


